package Shop.service;

import Shop.damain.Goods;
import Shop.damain.Orders;
import Shop.damain.Users;
import Shop.tools.DBHelper;
import Shop.tools.OrdersTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by BadHuge on 2020/9/11.
 */
public class GoodService {
 Orders orders = new Orders();

    public List<Goods> QueryGoodsByPage(int page) {
        List<Goods> goodsList=new ArrayList<>();
        String sql="select * from goods limit "+page+",5";
        List<Map<String,Object>> data = DBHelper.query(sql);
        for (Map<String,Object> row: data) {
            Goods goods= new Goods(
                    Integer.parseInt(row.get("gpid").toString())
                    ,row.get("gname").toString()
                    ,Integer.parseInt(row.get("gprice").toString())
                    ,row.get("gtype").toString()
                    ,Integer.parseInt(row.get("ownerid").toString()));
            goodsList.add(goods);
        }
        return goodsList;
    }

    public List<Goods> QueryGoods() {
        List<Goods> goodsList=new ArrayList<>();
        String sql="select * from goods";
        List<Map<String,Object>> data = DBHelper.query(sql);
        for (Map<String,Object> row: data) {
            Goods goods= new Goods(
                    Integer.parseInt(row.get("gpid").toString())
                    ,row.get("gname").toString()
                    ,Integer.parseInt(row.get("gprice").toString())
                    ,row.get("gtype").toString()
                    ,Integer.parseInt(row.get("ownerid").toString()));
            goodsList.add(goods);
        }
        return goodsList;
    }

    public  Goods QueryGoodsByid(int id){
        String sql="select * from goods where id="+id;
        List<Map<String,Object>> data = DBHelper.query(sql);
        Map<String,Object> row = data.get(0);
        return new Goods(
                Integer.parseInt(row.get("gpid").toString())
                ,row.get("gname").toString()
                ,Integer.parseInt(row.get("gprice").toString())
                ,row.get("gtype").toString()
                ,Integer.parseInt(row.get("ownerid").toString()));

    }

    public int insertOrders(Goods goods) {
        int i;
        //Goods good = QueryGoodsByid(id);
        int oid= Integer.parseInt(OrdersTools.createOrderCode().toString());
        String sql="insert into orders (oid,gcount,ostates,spowner,gid) values ("+oid+",1,0,1,"+goods.getGpid()+")";
       i= DBHelper.executeUpdata(sql);
       if (i==0)
           return 0;
       else
           return oid;
    }
    public void insertOrdersToShop(int uid,int oid){
        String sql = "insert into shoppings (state,ownerid,oid) values (0,"+uid+","+oid+")";
        DBHelper.executeUpdata(sql);
    }

    public int QueryGoodCounts() {
        String sql="select count(*) from goods where 1=1";
        List<Map<String,Object>> data=DBHelper.query(sql);
        Map<String,Object> row = data.get(0);
        return Integer.parseInt(row.get("count(*)")+"" );
    }
}
